package com.example.gulimall.search.thread;

import org.apache.tomcat.util.threads.ThreadPoolExecutor;

import java.util.concurrent.*;

/**
 * 线程测试
 * @author GuLin
 * @date 2021-06-25 16:24
 */
public class ThreadTest {

   //线程池
   public static  ExecutorService service = Executors.newFixedThreadPool(10);

    /**
      1.初始化线程的4中方法
         1.1继承Thread
         1.2实现Runnable接口
         1.3实现Callable接口 + FutureTask （可以拿到返回结果，可以处理异常）
         1.4线程池,可以使用以下两种方式操作
             ==>Executors.newFiexedThreadPool(3);
             ==>new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit unit, workQueue, threadFactory, handler);
        总结: 方式1.1、1.2主线程无法获取线程的运算结果，方式1.3可以获取线程中的运算结果，但是不利于控制服务器中的线程资源，容易导致服务器资源耗尽
             方式1.4通过线程性能稳定，也可以获取执行结果，并捕获到异常，但是在业务复杂的情况下一个异步调用可能会依赖另一个异步调用的执行结果
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main....start");

        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程id===>" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("处理结果===>" + i);
            return  i;
        }, service);
        System.out.println(supplyAsync.get());

//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程id===>" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("处理结果===>" + i);
//        }, service);

        //1.4线程池,可以使用以下两种方式操作
//        service.execute(new Runnable01());
//        Future<Integer> submit = service.submit(new Callable01());
//        System.out.println(submit);

        //1.3实现Callable接口 + FutureTask （可以拿到返回结果，可以处理异常）
//        main....start
//        当前线程id===>1
//        处理结果===>5
//        获取回调函数结果===>5
//        main...end
//        Callable01 callable01 = new Callable01();
//        try {
//            Integer call = callable01.call();
//            System.out.println("获取回调函数结果===>"+call);
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }

        // 1.2实现Runnable接口
//        main....start
//        当前线程id===>1
//        处理结果===>5
//        main...end
//        Runnable01 runnable01 = new Runnable01();
//        runnable01.run();

        //1.1继承Thread线程
//        main....start
//        main...end
//        当前线程id===>12
//        处理结果===>5
//        Thread01 thread01 = new Thread01();
//        thread01.start();


        System.out.println("main...end");
    }

    private static void threadPool(){
        ExecutorService threadPool = new ThreadPoolExecutor(
                200,
                10,
                10L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<Runnable>(10000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        //定时任务的线程池
        ExecutorService service = Executors.newScheduledThreadPool(2);
    }

    public static class Callable01 implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            System.out.println("Callable01当前线程id===>"+Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("处理结果===>"+i);
            return i;
        }
    }

    public static class Runnable01 implements Runnable{
        @Override
        public void run() {
            System.out.println("Runnable01当前线程id===>"+Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("处理结果===>"+i);
        }
    }

    public static  class Thread01 extends Thread{
        @Override
        public void run() {
            System.out.println("Thread01当前线程id===>"+Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("处理结果===>"+i);
        }
    }
}
